package org.outlook.apps.goal.db;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.outlook.apps.goal.pojo.BaseMapping;
import org.outlook.apps.goal.pojo.Column;
import org.outlook.apps.goal.pojo.Goal;
import org.outlook.apps.goal.pojo.Mapping;
import org.outlook.commons.pojo.Globals;

public class DataSourceServiceImpl implements DataSourceService {

	public class GoalMapping extends BaseMapping<Goal> {
		
		public GoalMapping (){
			columns = new Column[7];
			columns[0] = new Column("id", Long.class, "id");
			columns[1] = new Column("title", String.class, "title");
			columns[2] = new Column("description", String.class, "description");
			columns[3] = new Column("level", Integer.class, "level");
			columns[4] = new Column("sequence", Integer.class, "sequence");
			columns[5] = new Column("createTimeStamp", String.class, "create_timeStamp");
			columns[6] = new Column("updateTimeStamp", String.class, "update_timeStamp");
		}

	}

	@Override
	public boolean authenticate(String username, String password) {
		//TODO
		String sql = "select count(*) from users where username=? and password=?";
		if(1 == Globals.db.queryCount(sql, new Object[]{username, password}))
			return true; 
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Goal> getGoalList() {
		String sql = "select * from goals";
		Mapping<Goal> goalMapping = new DataSourceServiceImpl.GoalMapping();
		List<Map<?,Object>> collection = Globals.db.query(sql, new Object[]{}, goalMapping.getColumns());
		if(null != collection) {
			List<Goal> list = new ArrayList<Goal>(collection.size());
			for(Map row : collection) {
				Map<String, Object> values = (Map<String, Object>)row;
				list.add(goalMapping.mapping(values, Goal.class));
			}
			return list;
		}
		return null;
	}

}
